<template>
  <div class="content">
    <div class="menu">
      <el-row :gutter="30">
        <el-col :span="2" :offset="0">
          <el-button
            type="primary"
            icon="el-icon-plus"
            @click="open_insert_view('add', null)"
            >添加</el-button
          >
        </el-col>
      </el-row>
    </div>

    <div class="table">
      <el-table :data="category_data" style="width: 100%" max-height="600px">
        <el-table-column
          prop="userid"
          label="编号"
          width="180"
        ></el-table-column>
        <el-table-column
          prop="username"
          label="用户名称"
          width="180"
        ></el-table-column>

        <el-table-column
          prop="departmentid"
          label="部门"
          width="180"
        > <template slot-scope="scope">
          {{department_data_list[scope.row.departmentid]}}
          </template>

        </el-table-column>
        <el-table-column
          prop="userlevel"
          label="用户等级"
          width="180"
        >
         <el-option label="活动审核员" :value="2">活动审核员 </el-option>
                <el-option label="活动发布员" :value="3">活动发布员 </el-option>
                <el-option label="普通用户" :value="4">普通用户 </el-option>
        <template slot-scope="scope">
          {{
          scope.row.userlevel == 1 ? '系统管理员'
          : scope.row.userlevel == 2 ? '活动审核员'
          : scope.row.userlevel == 3? '活动发布员'
          : '普通用户'  }}
          </template>



        </el-table-column>

        <el-table-column label="操作" width="340">
          <template slot-scope="scope">
            <el-button
              size="mini"
              @click="open_updateAuthor_view('updateAuthor', scope.row)"
              >授权</el-button
            >
            <el-button
              size="mini"
              @click="open_update_view('update', scope.row)"
              >更改部门</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="delete_data(scope.row.userid)"
              >删除</el-button
            >
             <el-button
              size="mini"
              @click="reset_data(scope.row.userid)"
              >重置密码</el-button>

          </template>
        </el-table-column>
      </el-table>

      <div class="insert_view">
        <el-dialog
          :title="current_form_data.userid == null ? '增加用户' : '增加用户'"
          :visible.sync="insert_view_display"
          top="1vh"
        >
          <el-form :model="current_form_data" :rules="rules" top="1vh">
            <el-form-item label="用户名" prop="title">
              <el-input
                v-model="current_form_data.username"
                autocomplete="off"
              ></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="title">
              <el-input
                v-model="current_form_data.password"
                autocomplete="off"
              ></el-input>
            </el-form-item>
            <el-form-item label="部门名称" prop="title">
              <el-select
                v-model="current_form_data.departmentid"
                placeholder="请选择"
              >
                <el-option
                  v-for="item in options"
                  :key="item.id"
                  :label="item.dname"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="insert_view_display = false">取 消</el-button>
            <el-button type="primary" @click="save_data">确 定</el-button>
          </div>
        </el-dialog>
      </div>

      <div class="update_view">
        <el-dialog
          :title="current_form_data.userid == null ? '更改部门' : '更改部门'"
          :visible.sync="update_view_display"
          top="1vh"
        >
          <el-form :model="current_form_data" :rules="rules" top="1vh">
            <el-form-item label="用户名" prop="title">
              <el-input
                disabled
                v-model="current_form_data.username"
                autocomplete="off"
              ></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="title">
              <el-input
                disabled
                v-model="current_form_data.password"
                autocomplete="off"
              ></el-input>
            </el-form-item>
            <el-form-item label="部门名称" prop="title">
              <el-select
                v-model="current_form_data.departmentid"
                placeholder="请选择"
              >
                <el-option
                  v-for="item in options"
                  :key="item.id"
                  :label="item.dname"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="update_view_display = false">取 消</el-button>
            <el-button type="primary" @click="update_data">确 定</el-button>
          </div>
        </el-dialog>
      </div>

      <div class="updateAuthor_view">
        <el-dialog
          :title="current_form_data.userid == null ? '更改权限' : '更改权限'"
          :visible.sync="updateAuthor_view_display"
          top="1vh"
        >
          <el-form :model="current_form_data" :rules="rules" top="1vh">
            <el-form-item label="用户名" prop="title">
              <el-input
                disabled
                v-model="current_form_data.username"
                autocomplete="off"
              ></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="title">
              <el-input
                disabled
                v-model="current_form_data.password"
                autocomplete="off"
              ></el-input>
            </el-form-item>
            <el-form-item label="部门名称" prop="title">
              <el-select
                disabled
                v-model="current_form_data.departmentid"
                placeholder="请选择"
              >
                <el-option
                  v-for="item in options"
                  :key="item.id"
                  :label="item.dname"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="权限" prop="title">
              <!-- <el-input
                v-model="current_form_data.userlevel"
                autocomplete="off"
              ></el-input> -->

              <el-select
                v-model="current_form_data.userlevel"
                placeholder="请选择等级"
              >
                <el-option label="活动审核员" :value="2">活动审核员 </el-option>
                <el-option label="活动发布员" :value="3">活动发布员 </el-option>
                <el-option label="普通用户" :value="4">普通用户 </el-option>
              </el-select>

            </el-form-item>
          </el-form>

          <div slot="footer" class="dialog-footer">
            <el-button @click="updateAuthor_view_display = false"
              >取 消</el-button
            >
            <el-button type="primary" @click="updateAuthor_data"
              >确 定</el-button
            >
          </div>
        </el-dialog>
      </div>
    </div>
  </div>
</template>

<script>
import {
  departmentSelectAll,
  departmentSave,
  updateUserDepartment,
  userSelectAll,
  addUser,
  userDeleteByID,
  getDepartmentSelectAll,
  resetUserUpdate
} from "@/api/requests/base";
export default {
  name: "user-manage",
  data() {
    return {
      category_data: [],
      insert_view_display: false,
      update_view_display: false,
      updateAuthor_view_display: false,
      current_form_data: {
      },
      department_data_list: {},
      rules: {},
      options: [],
    };
  },
  created() {
    this.get_department_data_all()


  },
  methods: {
    //发送请求拿到部门信息
    get_department_data_all() {
      getDepartmentSelectAll().then((res) => {
        var dep = JSON.stringify(res.data);
        console.log("部门表的信息" + dep);
        this.options = res.data;
         this.options.forEach(item => {
          this.department_data_list[item.id] = item.dname
        })
             this.get_user_data_all()
        });
    },


    get_user_data_all() {
      userSelectAll().then((res) => {
        this.category_data = res.data;
      });
    },

    open_insert_view(type, data) {
      if (type == "add") {
        this.current_form_data = {};
      }
      this.insert_view_display = true;
    },

    open_update_view(type, data) {
      this.current_form_data = data;
      this.update_view_display = true;
    },

    open_updateAuthor_view(type, data) {
      this.current_form_data = data;
      this.updateAuthor_view_display = true;
    },
    updateAuthor_data() {
      updateUserDepartment(this.current_form_data).then((res) => {
        if (res.status) {
          this.$notify({
            title: "成功",
            message: "操作成功",
            type: "success",
          });
        } else {
          this.$notify({
            title: "失败",
            message: "修改部门失败",
            type: "fail",
          });
        }
      });
    },
    update_data() {
      updateUserDepartment(this.current_form_data).then((res) => {
        if (res.status) {
          this.$notify({
            title: "成功",
            message: "操作成功",
            type: "success",
          });
        } else {
          this.$notify({
            title: "失败",
            message: "修改部门失败",
            type: "fail",
          });
        }
      });
    },
    save_data() {
      addUser(this.current_form_data).then((res) => {
        this.$notify({
          title: "成功",
          message: "操作成功",
          type: "success",
        });

        this.insert_view_display = false;
        this.get_user_data_all();
      });
    },

    //删除功能完毕
    delete_data(id) {
      this.$confirm("确定要删除这个用户吗, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          userDeleteByID(id).then((res) => {
            console.log(id);
            this.$notify({
              title: "成功",
              message: "操作成功",
              type: "success",
            });
            this.get_user_data_all();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    reset_data(id) {
      this.$confirm("确定要重置这个用户吗, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          resetUserUpdate(id).then((res) => {
            console.log(id);
            this.$notify({
              title: "成功",
              message: "操作成功",
              type: "success",
            });
            this.get_user_data_all();
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消重置",
          });
        });
    },
  },
};
</script>


<style lang="scss" scoped>
.content {
  padding: 50px;
  .el-dialog {
    overflow: auto;
  }
  .el-table {
    margin-top: 30px;
  }
}
</style>
